package com.shz.appletsapi.config.security.social.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.social.connect.Connection;
import org.springframework.web.servlet.view.AbstractView;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonConnectView extends AbstractView {
	
	private ObjectMapper objectMapper = new ObjectMapper();

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		Map<String, List<Connection<?>>> connections  = (Map<String, List<Connection<?>>>) model.get("connectionMap");
		Map<String,Boolean> result =new HashMap<>();
		for(String key: connections.keySet()) {
			List<Connection<?>> listConnection = connections.get(key);
			result.put(key,(listConnection != null && listConnection.size() >0));
		}
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().write(objectMapper.writeValueAsString(result));
		
	}

}
